package com.cda.common;

import com.sun.jna.*;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.PointerByReference;
import org.springframework.beans.factory.annotation.Value;

import java.util.Arrays;
import java.util.List;


/**
 * Created by peiming on 2019/3/3.
 */
public class CommandPos3 {
    /**
     * DLL动态库调用方法2
     */
    @Value("${dll.open.url}")
    private static String dllUrl;

    public interface CLibrary extends Library {


        CLibrary INSTANCE = (CLibrary) Native.loadLibrary("D:\\sf\\trunk2\\sf-client\\lib\\test\\MisPos.dll", CLibrary.class);

        Integer BankTrans(InputData InputData[], Pointer pointer);
        Integer BankTrans(InputData.ByReference inputData, Pointer pointer);
        Integer BankTrans(InputData InputData, PointerByReference pointer);
        Integer BankTrans(InputData InputData, OutPutData pointer,PointerByReference reference);
        Integer BankTrans(String InputData, Pointer pointer);
        Integer BankTrans(byte[] InputData, Pointer pointer);
        Integer BankTrans(Pointer InputData, Pointer pointer);
        Integer BankTrans(InputData InputData, Pointer pointer);
        Integer BankTrans(PointerByReference InputData, Pointer pointer);
        Integer BankTrans(ByteByReference InputData, Pointer pointer);
        public static class InputData extends Structure {
            public byte[] posid=new byte[8]; /* 收银机号(8 字节,左对齐,不足部分补空格)*/
            public byte[] operid=new byte[8]; /* 操作员号(8 字节,左对齐,不足部分补空格)*/
            public byte[] trans=new byte[2]; /* 交易类型: '00'消费'01'撤销'02'退货'03'查余*/
            public byte[] amount=new byte[12]; /* 金额(12 字节,无小数点,左补 0,单位:分)*/
            public byte[] old_date=new byte[8]; /* 原交易日期(8 字节,yyyymmdd 格式,隔日退货时用)*/
            public byte[] old_reference=new byte[12]; /* 原交易参考号*/
            public byte[] old_trace=new byte[6]; /* 流水号(6 字节,右对齐,左补 0,退货或重打印等用)*/
            public byte[] old_batch=new byte[6]; /*57~62 位 原批次号*/
            public byte[] old_auth=new byte[6]; /*63~68 位 原授权码*/
            public byte[] old_terno=new byte[8]; /*69~76 位 原交易终端号*/
            public byte[] szFenqiNum=new byte[2]; /*77~78 位 分期数 03，06，09，12，18，24，36，42，48，60*/
            public byte[] szServiceNum=new byte[2];/*79~80 位 享受服务人数*/
            public byte[] szGoodsNo=new byte[2]; /*81~92 位 商品项目编码*/
            public byte[] trk2=new byte[37]; /* 二磁道数据（37 字节，左对齐，不足部分补空格）*/
            public byte[] trk3=new byte[104]; /* 三磁道数据（104 字节, 左对齐，不足部分补空格）*/
            public byte[] lrc=new byte[3]; /* 交易校验数据（3 位从 0～9 的随机字符）*/
            public byte[] szOrderTrace=new byte[20]; // 6/16/2015 新增 收银流水(订单)号
            public byte[] szPrefer=new byte[50]; // 优惠券 [5/24/2017 Xuwb]
            public byte[] szRsv=new byte[300]; // 10/25/2015 新增 保留字段 在此输入二维码的条码数据 不足
            public static class ByReference extends InputData implements Structure.ByReference {}
            public static class ByValue extends InputData implements Structure.ByValue {}
            @Override
            protected List getFieldOrder() {
                return Arrays.asList(new String[]{"posid", "operid", "trans","amount", "old_date", "old_reference","old_trace","old_batch", "old_auth",
                        "old_terno","szFenqiNum", "szServiceNum", "szGoodsNo","trk2", "trk3", "lrc","szOrderTrace", "szPrefer", "szRsv"});
            }

        }
      /*  public static class OutPutData extends Structure{
            public byte[] resp_code=new byte[2]; *//*1～2 位 返回码 (2 字节，"00"交易成功,其他失败)*//*
            public byte[] bank_code=new byte[4]; *//*3～6 位 银行行号（4 字节）*//*
            public byte[] card_no=new byte[30]; *//*7～36 位 卡号:622280*********4860 *//*
            public byte[] expr=new byte[4]; *//*37～40 位 有效期 (4 字节) *//*
            public byte[] amount=new byte[12]; *//*141～52 位 金额（12 字节，无小数点，左补 0，单位：分）*//*
            public byte[] trace=new byte[6]; *//*53～58 位 流水号 (6 字节，左对齐)*//*
            public byte[] refer=new byte[12]; *//*59～70 位 交易参考号*//*
            public byte[] auth=new byte[6]; *//*71～76 位 授权号*//*
            public byte[] batch=new byte[6]; *//*77～82 位 批次号*//*
            public byte[] date=new byte[8]; *//*83～96 位 交易日期（8 字节,yyyymmddhhmmss 格式）*//*
            public byte[] userno=new byte[15]; *//*97～111 位 商户号*//*
            public byte[] terno=new byte[8]; *//*112～119 位终端号*//*
            public byte[] old_terno=new byte[8]; *//*120～127 位 原终端号*//*
            public byte[] resp_chin=new byte[50]; *//* 错误说明(左对齐，不足部分补空格)*//*
            public byte[] OrderNo=new byte[50]; *//* 二维码订单号 *//*
            public byte[] lrc=new byte[3]; *//* 交易数据校验码（3 字节）*//*
            public static class ByReference extends OutPutData implements Structure.ByReference {}
            public static class ByValue extends OutPutData implements Structure.ByValue {}
            @Override
            protected List getFieldOrder() {
                return Arrays.asList(new String[]{"resp_code", "bank_code", "card_no","expr", "amount", "trace","refer", "auth",
                        "batch","date", "userno", "terno","old_terno", "resp_chin", "OrderNo","lrc"});
            }

        }*/
    }

    public static void main(String[] arg) throws Exception{
        System.setProperty("jna.encoding","UTF-8");
        String dataStr="D2015 A001 0300000"+"000000000001"+"11900000000016700000000009800000000000272030 5000000000015 333333333333333333333333333333333333333333333333333333333333333 33333333333333333333333333333333333333333 W20150616205755001 ";
       for(int i=0;i<=1;i++) {
            CLibrary.InputData.ByReference inputData = new CLibrary.InputData.ByReference();
          /* inputData.posid=Arrays.copyOf(dataStr.getBytes(),214);
           inputData.operid=Arrays.copyOf("01 03000".getBytes(),8);*/
           inputData.amount=Arrays.copyOf("000000000001".getBytes(), 12);
           inputData.trans=Arrays.copyOf("00".getBytes(), 2);
           CLibrary.InputData.ByReference[] inputDatas =(CLibrary.InputData.ByReference[])inputData.toArray(1);

           inputDatas[0].amount=Arrays.copyOf("000000000001".getBytes(), 12);
           inputDatas[0].trans=Arrays.copyOf("00".getBytes(), 2);
           /*inputData.old_date=Arrays.copyOf("11900000".getBytes(),8);
           inputData.old_reference=Arrays.copyOf("000016700000".getBytes(),12);
           inputData.old_trace=Arrays.copyOf("000009".getBytes(),6);
           inputData.old_batch=Arrays.copyOf("800000".getBytes(),6);
           inputData.old_auth=Arrays.copyOf("000000".getBytes(),6);
           inputData.old_terno=Arrays.copyOf("03".getBytes(),2);
           inputData.szFenqiNum=Arrays.copyOf("A2015 A0".getBytes(),8);
           inputData.operid=Arrays.copyOf("B2015 A0".getBytes(),8);
           inputData.operid=Arrays.copyOf("C2015 A0".getBytes(),8);
           inputData.operid=Arrays.copyOf("E2015 A0".getBytes(),8);
           inputData.operid=Arrays.copyOf("F2015 A0".getBytes(),8);
           inputData.operid=Arrays.copyOf("G2015 A0".getBytes(),8);*/


           Pointer result1 =new Memory(1024);
           inputData.getPointer().setPointer(200,new Memory(1024));
           inputData.write();
        //  Pointer pointer=inputData.getPointer();
           //pointer.setString(0,dataStr,"GBK");
           PointerByReference pointerByReference=new PointerByReference();
           PointerByReference returnPonter=new PointerByReference();
           //pointerByReference.setPointer(pointer);
           returnPonter.setPointer(new Memory(1024));
           Pointer result =new Memory(1024);
           inputData.getPointer().setPointer(0,new Memory(1024));
          int a= CLibrary.INSTANCE.BankTrans(dataStr,result);

          // int a= CLibrary.INSTANCE.BankTrans(dataStr,result);System.out.println(result.getByteArray(0, 1024).toString());
        System.out.println("outPutData:" + new String(result.getByteArray(0, 1024), "GBK"));
        long peer1=Pointer.nativeValue(result);
        Native.free(peer1);
        Pointer.nativeValue(result, 0);
           long peer2=Pointer.nativeValue(pointerByReference.getPointer());
           Native.free(peer2);
           Pointer.nativeValue(result1, 0);
           System.gc();
        String str="0001026212263602090193574           26110000000000010000439081021109620123450000012019032302540910500008999914010064157        交易成功                                          42BA2F76D9B12CD7CC39C5D1689CBD4E                  00                                                                                                                                                                                                                                                                                                                                                                                                                                                   d  d漙   o\\ X$] 8 6 ) \\ I n t e l \\ O p e n C L   S D K \\ 2 . 0 \\ b i n \\ x 6 4 ; C : \\ P r o g r a m   F i l e s \\ I n t e l \\ I n t e l ( R )   M a n a g e m e n t   E n g i n e   C o m p o n e n t s \\ D A L ; C : \\ P r o g r a m   F i l e s \\ I n t e l \\ I n t e l ( R )   M a n a g e m e n t   E n g i n e   C o m p o n e n t s \\ I P T ; C : \\ P r o g r a \n";
           System.out.println("i="+i);
        }

}
   /* public static Map<String,String> Paymonney(String amount) throws Exception{
           Map<String,String> map=new HashMap<>();
            CLibrary.InputData.ByValue InputData = new CLibrary.InputData.ByValue();
            InputData.amount=Arrays.copyOf(amount.getBytes(), 12);  ;
            InputData.trans=Arrays.copyOf("00".getBytes(), 2);
            Pointer result =new Memory(1024);
            int a= CLibrary.INSTANCE.BankTrans(InputData,result);
            System.out.println("a:"+a);
            System.out.println(result.getByteArray(0, 1024).toString());
            System.out.println("outPutData:" + new String(result.getByteArray(0, 1024), "GBK"));
        map.put("code", a+"");
        map.put("content",new String(result.getByteArray(0, 1024), "GBK"));
            System.gc();
            //Pointer p1 =new Memory(1024);
            long peer1=Pointer.nativeValue(result);
            Native.free(peer1);
            Pointer.nativeValue(result, 0);
            //String str="0001026212263602090193574           26110000000000010000439081021109620123450000012019032302540910500008999914010064157        交易成功                                          42BA2F76D9B12CD7CC39C5D1689CBD4E                  00                                                                                                                                                                                                                                                                                                                                                                                                                                                   d  d漙   o\\ X$] 8 6 ) \\ I n t e l \\ O p e n C L   S D K \\ 2 . 0 \\ b i n \\ x 6 4 ; C : \\ P r o g r a m   F i l e s \\ I n t e l \\ I n t e l ( R )   M a n a g e m e n t   E n g i n e   C o m p o n e n t s \\ D A L ; C : \\ P r o g r a m   F i l e s \\ I n t e l \\ I n t e l ( R )   M a n a g e m e n t   E n g i n e   C o m p o n e n t s \\ I P T ; C : \\ P r o g r a \n";
        return  map;
    }*/
}